<?php

namespace Dcat\Admin\Caijiwebinfo\Http\Controllers;

use Dcat\Admin\Caijiwebinfo\Models\CaijiwebinfoCms;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Admin;
use Dcat\Admin\Caijiwebinfo\Forms\GetWxgzhArt;
use Dcat\Admin\Widgets\Modal;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Tab;
use Dcat\Admin\Widgets\Alert;
use Dcat\Admin\Caijiwebinfo\Models\CaijiwebinfoMedia;

class CaijiwebinfoController extends AdminController
{
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header('采集文章管理')
            ->description('全部')
            ->breadcrumb(['text'=>'列表','uri'=>''])
            ->body(view('ycookies.caijiwebinfo::collapse'))
            ->body($this->grid());
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        //只有默认情况下
        if(!request()->has('title')){
            // 根据不同的登陆用户 给不同的条件
            if(Admin::user()->type == 'A') $req = request()->merge(['title'=> '他信']);
            if(Admin::user()->type == 'B') $req = request()->merge(['title'=> '大数据反腐']);
        }

        $grid =  Grid::make(new CaijiwebinfoCms(), function (Grid $grid) {
            $grid->model()->orderBy('id','DESC');
            $grid->column('id')->sortable();
            $grid->column('web_url','采集地址')->link();
            $grid->column('title','标题');
            $grid->column('platform','平台名')->using(CaijiwebinfoCms::Platform_arr);
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                //$filter->expand(true);
                $filter->equal('id')->width(3);
                $filter->like('web_url','采集地址')->width(3);
                $filter->like('title','标题')->width(3);
            });
        });
        $tab = Tab::make();
        $tab->add('信息列表', $grid, true);
        $tab->addLink('资源管理', admin_url('/caijiwebinfo/media'));
        $card =  Card::make('',$tab);
        return $card;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CaijiwebinfoCms(), function (Show $show) {
            $show->field('id');
            $show->field('title','文章标题');
            $show->field('web_url','采集地址')->link();
            $show->field('platform','网络平台')->using(CaijiwebinfoCms::Platform_arr);
            $show->field('content','文章内容')->unescape();
            $show->field('created_at');
            //$show->field('updated_at');
            $show->disableListButton();
            $show->disableDeleteButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CaijiwebinfoCms(), function (Form $form) {
            $form->display('id');
            $form->hidden('user_id')->default(1);
            $form->hidden('web_url')->default('');
            $form->hidden('official_accounts')->default('');
            $form->hidden('platform')->default('');
            $form->caijiwebinfo('采集web信息') // 定义按钮文字
                ->icon('feather icon-crosshair') // 定义按钮图标
                ->titleClass('art_title') // 插入文章标题到对应的class
                ->contentClass('art_content'); // 插入文章内容到对应的class

            $form->text('title','文章标题')->setElementClass('art_title');
            // todo 百度编辑器
            //$form->ueditor('content','文章内容')->setElementClass('art_content');
            // todo 135编辑器
            $form->neditor('content','文章内容')->setElementClass('art_content');
            // todo tinymce编辑器
            //$form->editor('content','文章内容')->setElementClass('art_content');
            $form->disableCreatingCheck();
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->display('created_at');
            // 保存前回调
            $form->saving(function (Form $form) {
                // 判断是否是新增操作
                if ($form->isCreating()) {
                    $caijiwebinfo_web_url = cache()->get('caijiwebinfo_web_url');
                    $count = CaijiwebinfoCms::where(['web_url'=> $caijiwebinfo_web_url])->count();
                    if(!empty($count)){
                        return $form->response()->error('因开启了采集日志,信息已经存在')->refresh();
                    }
                    $caijiwebinfo_platform = cache()->get('caijiwebinfo_platform');
                    if(!empty($caijiwebinfo_web_url)){
                        $form->web_url = $caijiwebinfo_web_url;
                        cache()->forget('caijiwebinfo_web_url');
                    }
                    if(!empty($caijiwebinfo_platform)){
                        $form->platform = $caijiwebinfo_platform;
                        cache()->forget('caijiwebinfo_platform');
                    }
                }
            });
            // 保存后回调
            $form->saved(function (Form $form, $result) {
                // 判断是否是新增操作
                if ($form->isCreating()) {
                    // 自增ID
                    $newId = $result;
                    CaijiwebinfoMedia::where(['caiji_web_url'=>$form->web_url])->update(['cms_id'=>$newId]);
                }
            });
            //$form->display('updated_at');
        });
    }

}